package com.example.ui.learn;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.example.DBHelper.Dao;
import com.example.downloadmanager.Downloader;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.huashangapp.R.drawable;
import com.example.ui.leftmenu.DownItem;
import com.example.ui.leftmenu.Itme10View;
import com.example.util.AsyncLoadImage;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class ListAdapter extends BaseAdapter implements OnClickListener {

	private Context context = null;
	List<String> adlist;
	Dao dao;
	private AsyncLoadImage asyncLoadImage;

	public ListAdapter(Context context, List<String> adlist) {
		this.context = context;
		this.adlist = adlist;
		 asyncLoadImage=new AsyncLoadImage();
		 dao=HSApplication.getDao();
	}
	@Override
	public int getCount() {
		return adlist.size();
	}
	@Override
	public View getItem(int position) {
		return this.getView(position, null, null);
	}
	@Override
	public long getItemId(int id) {
		return id;
	}
	@SuppressLint({ "WrongViewCast", "ShowToast" })
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView=null;
		if (convertView == null) {
			convertView = View.inflate(this.context, R.layout.item10_listitem,
					null);
		}
		String strUrl=adlist.get(position);
		DownItem downItem = DownUtils.listDownItem.get(strUrl);
		// 课程名称
		TextView Title = (TextView) convertView.findViewById(R.id.txt1);
		// 第几讲名称
		TextView Sub_title = (TextView) convertView.findViewById(R.id.txt2);
		TextView Name = (TextView) convertView.findViewById(R.id.txt5);
		ImageView Teacher_pic = (ImageView) convertView
				.findViewById(R.id.teacher_pic);
		Title.setText(downItem.getTitle());
		Sub_title.setText(downItem.getSub_title());
		Name.setText(downItem.getName());
		asyncLoadImage.imageLoader.displayImage(downItem.getPic_Url(), Teacher_pic,asyncLoadImage.options);
		TextView jindu=(TextView)convertView.findViewById(R.id.txt3);
		TextView bai=(TextView)convertView.findViewById(R.id.txt4);
		// 下载的进度1.2M/6M
		jindu.setText(downItem.getJindu().getText());
		downItem.setJindu(jindu);
		bai.setText(downItem.getBai().getText());
		// 下载的百分比20%
		downItem.setBai(bai);
		// 讲课老师的名字
		ProgressBar pBar=(ProgressBar) convertView
				.findViewById(R.id.progressBar);
		//pBar.setProgress(downItem.getPd_progressBar().getProgress());
		downItem.setPd_progressBar(pBar);
		downItem.getPd_progressBar().setMax(downItem.getFileSize());
		downItem.getPd_progressBar().setProgress(downItem.getDownLoadFileSize());
		downItem.setBtn_pause((Button) convertView
				.findViewById(R.id.btn_pause));
		downItem.setBtn_continue((Button) convertView
				.findViewById(R.id.btn_continue));
		downItem.setBtn_delete((Button) convertView
				.findViewById(R.id.btn_delete));
		Toast.makeText(context, "进度条"+pBar.getProgress()+"大小"+pBar.getMax(), Toast.LENGTH_LONG);
		// 给统一列表项的下载、停止、暂停、继续、进度条控件设置相同的Tag
		downItem.getBtn_continue().setTag(strUrl);
		downItem.getBtn_pause().setTag(strUrl);
		downItem.getBtn_delete().setTag(strUrl);
		downItem.getBtn_pause().setOnClickListener(this);
		downItem.getBtn_continue().setOnClickListener(this);
		downItem.getBtn_delete().setOnClickListener(this);
		
		
		if(downItem.getIsDowned()){
		 if(!downItem.isPause){
			 downItem.getBtn_pause().setVisibility(0);
			 downItem.getBtn_continue().setVisibility(4);
			 downItem.getBtn_delete().setVisibility(4);
		 }else{
			 downItem.getBtn_pause().setVisibility(4);
			 downItem.getBtn_continue().setVisibility(0);
			 downItem.getBtn_delete().setVisibility(4);
		 }
		}else{
			downItem.getBtn_pause().setVisibility(4);
			downItem.getBtn_continue().setVisibility(0);
			downItem.getBtn_delete().setVisibility(4);
		}
		if(downItem.isChange){
			downItem.getBtn_pause().setVisibility(4);
			downItem.getBtn_continue().setVisibility(4);
			downItem.getBtn_delete().setVisibility(0);
		}
		return convertView;
	}
	DownItem downitem ;
	private File f;
	@Override
	public void onClick(View v) {
		int index = DownUtils.downUrl.indexOf(v.getTag());
		 downitem = DownUtils.listDownItem.get(v.getTag());
		switch (v.getId()) {
		case R.id.btn_pause:
			downitem.pause();
			downitem.getBtn_pause().setVisibility(4);
			downitem.getBtn_continue().setVisibility(0);
			downitem.getBtn_delete().setVisibility(4);
			break;
		case R.id.btn_continue:
			if(downitem.isDowned){
				downitem.continued();
				DownUtils.downloaders.get(v.getTag()).init();
				downitem.getBtn_pause().setVisibility(0);
				downitem.getBtn_continue().setVisibility(4);
				downitem.getBtn_delete().setVisibility(4);
			}else{
				if(DownUtils.downIngCount<DownUtils.downCount){
				downitem.isDowned=true;
				dao.deleteListenDown((String) v.getTag());
				DownUtils.downIngCount++;
				DownUtils.downloaders.get(v.getTag()).init();
				downitem.getBtn_pause().setVisibility(0);
				downitem.getBtn_continue().setVisibility(4);
				downitem.getBtn_delete().setVisibility(4);
				}
			}
			break;
		case R.id.btn_delete:
			String lisname=downitem.getUrl().substring(downitem.getUrl().lastIndexOf("/") + 1);
			 f=new File(ListeningActivity.filepath+lisname);
			new AlertDialog.Builder(context)
			.setTitle("该文件正在下载是否删除！")
			.setNegativeButton(
							"取消",
							new android.content.DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									return;
								}
							})
			.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog,
								int which) {
							if(DownUtils.listDownItem.get(downitem.getUrl()).isDowned){
								
								DownUtils.downIngCount--;
								dao.deleteListenDown(downitem.getUrl());
								}
									DownUtils.listDownItem.remove(downitem);
									DownUtils.downloaders.remove(downitem.getUrl());
									DownUtils.downUrl.remove(downitem.getUrl());
									f.delete();
									dao.delete(downitem.getUrl());
									if(DownUtils.downloaders.size()<=0){
											dao.closeDb();
									}
									h.sendEmptyMessage(0);
						}
					}).show();
			
			break;
		}
	}
	Handler h=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				Itme10View.down_list.setAdapter(DownUtils.adapter);
				DownUtils.adapter.notifyDataSetChanged();
				break;
			case 1:
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
		
	};
}
